<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use App\Model\AdminUser;
use \Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{

    public function index(Request $request)
    {
        if ($request->method() == 'GET'){
            return view('admin.auth.login');
        }

        $validator =  Validator::make($request->all(),[
            'username' => 'required',
            'password' => 'required'
        ]);

        if($validator->fails())
        {
            return redirect()->back()
                ->withErrors(['用户名或密码错误！'])
                ->withInput();
        }

        if ($this->adminAuth()->attempt($request->only('username','password'))){
            return redirect(url('admin/'));
        }

        return redirect()->back()
            ->withErrors(['用户名或密码错误！'])
            ->withInput();
    }

    public function logout()
    {
        $this->adminAuth()->logout();
        return redirect(url('admin/login'));
    }
}
